package edu.hm.activebadge.rfid.acquisition.badge;

import java.awt.Color;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.swing.JTextField;

import edu.hm.activebadge.rfid.adaption.server.AlarmMessage;
import edu.hm.activebadge.rfid.adaption.server.ServerParameter;
import edu.hm.activebadge.rfid.interpretation.action.InterpretedAction;
import edu.hm.activebadge.rfid.interpretation.tagclass.ITaggedEntity;
import edu.hm.activebadge.rfid.representation.IContextRepresentation;

public class TCPReceive implements Runnable {
	private BadgeParameter params = new BadgeParameter();
	
	private ServerSocket ss;
	JTextField text;

	public TCPReceive(JTextField t){
		text =t;
		try {
			ss = new ServerSocket(Integer.parseInt(params.getClientPort()));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		while(true){
			try{
			Socket s = ss.accept();
			InputStream is = s.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);

			AlarmMessage alarm = (AlarmMessage) ois.readObject();
			
			if(alarm.isActive())
			{
				text.setText("NOTFALL! Sofort nach"+ alarm.getRoom()+" zu "+alarm.getPatient()+" kommen!");
				text.setBackground(new Color(255,165,0));
				Toolkit.getDefaultToolkit().beep();
			}
			else
			{
				text.setText("Entwarnung! Personal bei Patienten "+alarm.getPatient()+" angekommen!");
				text.setBackground(new Color(0,0,0));
				Toolkit.getDefaultToolkit().beep();
			}
			
			}catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
}
